package createalias;

## This file is part of the Simplemail project.
#  Copyright (c) 2008 the Simplemail Team (see authors).
# 
#  For the full copyright and license information, please view the COPYING
#  file that was distributed with this source code. You can also view the
#  COPYING file online at http://unixforum.org.ua/misc/COPYING.txt
# 
#  @copyright  (c) the authors
#  @author     Dmitriy Shilenko <rtyug@ukr.net>
#  @license    GPLv2 http://unixforum.org.ua/misc/COPYING.txt
#  @package    Panel Mailbox
#  @version    $Id: $

use strict;
use CGI::Carp qw(fatalsToBrowser);
use Data::Validate::Domain qw(is_domain);
use Data::Validate::Email qw(is_email);
use base 'func';

sub new {
    my $self = shift->SUPER::new(@_);
    $self->dbi_connect if $self->can("dbi_connect");
    return $self;
}

sub return_create_alias2 {

    my $self = shift;
    my $dd;

    if (   ( !length( $self->{p}->{fDomain} ) )
        || ( 2 > length( $self->{p}->{fDomain} ) )
        || ( 250 < length( $self->{p}->{fDomain} ) )
        || !is_domain( $self->{p}->{fDomain} ) )
    {
        $self->{he}->{fDomain_correct} = 1;
    }
    if ( $self->{he}->{fDomain_correct} != 1 ) {

        my $sth = $self->{dbh}->prepare(qq{
            SELECT domain,
                                 (SELECT count(*) 
                                 FROM alias 
                                 WHERE domain = ?) AS c_alias,
                                 aliases
                               
                         FROM domain
                         WHERE domain = ? 
                         AND idnane = ?  limit 1
                             }
        );
        $sth->execute(
            $self->{p}->{fDomain},
            $self->{p}->{fDomain},
            $self->{session}->{user}
        );
        $dd = $sth->fetchrow_hashref();
        $sth->finish();

        if ( $dd->{domain} ne $self->{p}->{fDomain} ) {

            $self->{he}->{ds_correct} = 1;
        }

        if ( $dd->{aliases} == -1 ) {
            $self->{he}->{alias_off} = 1;
        }
        else {
            if (   ( $dd->{aliases} != 0 )
                && ( $dd->{aliases} <= $dd->{c_alias} ) )
            {
                $self->{he}->{alias_limit} = 1;
            }
        }

    }

    if ( is_email( $self->{p}->{fAddress} . '@' . $self->{p}->{fDomain} ) ) {

        my $sth = $self->{dbh}->prepare(
            'SELECT username
                             FROM domain AS t1,
                                  mailbox AS t2
                             WHERE t2.username = ? 
                             AND t1.idnane = ?  
                             LIMIT 1
                             '
        );
        $sth->execute( $self->{p}->{fAddress} . '@' . $self->{p}->{fDomain},
            $self->{session}->{user} );
        my $mailbox_sql = $sth->fetchrow_array();
        $sth->finish();

        if (  $mailbox_sql eq $self->{p}->{fAddress} . '@'
            . $self->{p}->{fDomain} )
        {
            $self->{he}->{fA_no_exist} = 1;
        }
        else {
            $self->{p}->{fAddress} =
              $self->{p}->{fAddress} . '@' . $self->{p}->{fDomain};
        }

    }

    elsif ( $self->{p}->{fAddress} eq '*' ) {

        #$fAddress =~ 's/*//g';
        $self->{p}->{fAddress} = '@' . $self->{p}->{fDomain};
    }

    else {
        $self->{he}->{fA_correct} = 1;
    }

    if (   ( $self->{p}->{fGoto} =~ /^\*@(.*)$/ )
        || ( is_email( $self->{p}->{fGoto} ) ) )
    {
        if ( $self->{p}->{fGoto} =~ /^\*@(.*)$/ ) {
            substr( $self->{p}->{fGoto}, 0, 1, "" );
        }
    }

    else {
        $self->{he}->{fGoto_correct} = 1;
    }

    my $fount;
    foreach my $value ( values( %{ $self->{he} } ) ) {
        if ( $value == 1 ) { $fount = 1; last; }
    }
    if ( !$fount ) {

        my $sth = $self->{dbh}->prepare(qq{
            INSERT INTO alias (address,goto,domain,created,modified) 
                             VALUES (?,?,?,DATETIME("NOW"),DATETIME("NOW"))
                            }
        );
        $sth->execute(
            $self->{p}->{fAddress},
            $self->{p}->{fGoto},
            $self->{p}->{fDomain}
        );
        $sth->finish();
        $self->{he}->{alias_added} = 1;
    }

    $self->{he}->{ALIAS} = '@' . $self->{p}->{fDomain};
    $self->{he}->{GOTO}  = $self->{p}->{fGoto};

    return $self;
}

sub return_create_alias {

    my $self = shift;

    my $sth = $self->{dbh}->prepare(
        'SELECT domain 
                         FROM domain 
                         WHERE idnane = ?
                             '
    );
    $sth->execute( $self->{session}->{user} );
    my $rows;
    push @{$rows}, $_ while $_ = $sth->fetchrow_hashref();
    $sth->finish();

    if ( !$rows ) {
        $self->{he}->{NO_DOMAIN} = 1;
    }
    else {
        $self->{he}->{ROWS} = $rows;
    }

    return $self;
}

sub templ_hash_create_alias {
    my $self = shift;

    $self->{session}->{filet} = 'create-alias';
    $self->SUPER::templ_hash(@_);

}

sub DESTROY {
    my $self = shift;
    $self->SUPER::DESTROY if $self->can("SUPER::DESTROY");
}

1;
